home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0197_Dynamic creation and circularly linking.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  2.1 KB  |  60 lines

  1.  
  2.  Question:  How do I make a simple method to rotate between forms?
  3.             How do I add my own return results to a ShowModal form?
  4.             How do I instantiate forms at runtime?
  5.  
  6.  Answer:  The method required is quite simple to implement.  For my
  7.     example I used 3 forms, the Mainform, Form1, and Form2.  I
  8.     placed a button on the Mainform that will bring up Form1, then
  9.     from that form you could rotate through any number of forms via
  10.     buttons placed on those forms.  For my example, only Form1 and
  11.     Form2 can be flipped between.
  12.  
  13.     step 1. Places these two lines in the interface section of this
  14.        Form, which will be refered to as the main form
  15.  
  16.        const
  17.          mrNext = 100;
  18.          mrPrevious = 101;
  19.  
  20.     step 2. On the main form add a button and add the following block
  21.         of code into it.
  22.  
  23.        var
  24.          MyForm: TForm;
  25.          R, CurForm: Integer;
  26.        begin
  27.           R := 0;
  28.           CurForm := 1;
  29.           while R <> mrCancel do begin
  30.             Case CurForm of
  31.               1: MyForm := TForm1.Create(Application);
  32.               2: MyForm := TForm2.Create(Application);
  33.             end;
  34.             try
  35.               R := MyForm.ShowModal;
  36.             finally
  37.               MyForm.Free;
  38.             end;
  39.             case R of
  40.               MrNext : Inc(CurForm);
  41.               MrPrevious : Dec(CurForm);
  42.             end;
  43.                 // these 2 lines will make sure we don't go out of bounds
  44.             if CurForm < 1 then CurForm := 2
  45.             else if CurForm > 2 then CurForm := 1;
  46.            end; // while
  47.          end;
  48.  
  49.     step 3. Add forms 1 and 2 (and any others you are going to have)
  50.         to the uses statement for the MainForm.
  51.  
  52.     step 4. On Form1 and Form2 add the MainForm to the uses (so they
  53.         can see the constants.
  54.  
  55.     step 5. On Form1, Form2 and all subsequent forms add 2 TBitBtn's,
  56.         labeled Next and Previous.  In the OnClick Events for these buttons
  57.         add the following line of code.
  58.           If it's a Next Button add :  ModalResult := mrNext;
  59.           If it's a Previous Button add :  ModalResult := mrPrevious;
  60.